package com.su.servlet;

import com.su.bean.User;
import com.su.service.UserService;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.Date;

@WebServlet(name = "AddUserInfoServlet", value = "/AddUserInfoServlet")
public class AddUserInfoServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        String userName = request.getParameter("userName");
        String userSex = request.getParameter("userSex");
        String userAd = request.getParameter("userAd");
        UserService userService = new UserService();
        User userInfo = new User();
        userInfo.setUsername(userName);
        userInfo.setBirthday(new Date());
        userInfo.setSex(userSex);
        userInfo.setAddress(userAd);
        System.out.println(userInfo);
        try {
            userService.insertUserInfo(userInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        request.getRequestDispatcher("/userInfoServlet").forward(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
